//
//  CommonTableViewController.m
//  HaHa
//
//  Created by lanou3g on 15/9/24.
//  Copyright (c) 2015年 吉亚平. All rights reserved.
//

#import "CommonTableViewController.h"

@interface CommonTableViewController ()
@property (nonatomic, strong) UITableView *CtableView;

@property (nonatomic, strong) NSMutableArray *oneArray;
@property (nonatomic, strong) NSMutableArray *twoArray;
@property (nonatomic, strong) NSMutableArray *threeArray;



@end

@implementation CommonTableViewController{
    NSInteger number;
    CGFloat heightAll;
}



- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;

    self.navigationController.navigationBar.translucent = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    [self setViewHeight];
    NSLog(@"%f",heightAll);
    self.headerView = [[headerView alloc] initWithFrame:CGRectMake(5, 0, kScreenWdth - 10,heightAll)];
    self.tableView.tableHeaderView =_headerView;
     
    [self.headerView setvAlueWithModel:_fModel];
    [self reloadCommentsAndJson];
    
    __block CommonTableViewController *root = self;
    [self.tableView addHeaderWithCallback:^{
        [root.tableView headerEndRefreshing];
    }];
    [self.tableView addFooterWithCallback:^{
        [root reloadCommentsAndJson];
        [root.tableView footerEndRefreshing];
    }];

    
    
}




- (void)setViewHeight
{
    NSString *widthTemp = [_fModel.large_image objectForKey:@"width"];
    NSString *imagehieghtTemp = [_fModel.large_image objectForKey:@"height"];
    NSInteger width = [widthTemp integerValue];
    NSInteger height = [imagehieghtTemp integerValue];
    //如果大于表视图的宽度 等比缩放到与表视图等宽
    CGFloat imageHeight;
    if (width > self.tableView.width) {
        CGFloat beishu = width / self.tableView.width;
        imageHeight = height / beishu;
    }else if (height == 0){
        imageHeight = self.tableView.width;
    }else{
        imageHeight = height;
    }
    //图片标题高度
    CGFloat Textheight = [self YYHreturnStringHeight:_fModel.text Width:kScreenWdth-20 Font:17];
   
    heightAll = 40 + 10 + 30 + Textheight + imageHeight;

    
}

- (void)setFrames
{
    self.headerView = [[headerView alloc] initWithFrame:CGRectMake(5, 0, kScreenWdth - 10, 350)];
    
 
    
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (self.modelArray.count == 0) {
        return @"暂无评论";
    }
        return @"最新评论";
    
    
}



//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    UIView *viewheader = [[UIView alloc] initWithFrame:CGRectMake(1, 1, kScreenWdth - 20, 40)];;
//    UILabel *commentTitle = [[UILabel alloc] initWithFrame:CGRectMake(5, 10, 200, 40)];
//    commentTitle.text = @"热门评论";
//    [viewheader addSubview:commentTitle];
//    
//    return viewheader;
//}


#pragma mark =======评论解析========
- (void)reloadCommentsAndJson
{
    [self.activity startAnimating];
    number += 30;
    NSString *url = [NSString stringWithFormat:@"%@%ld%@%@%@",kCommentsA,number,kCommentsB,_fModel.group_id,kCommentsC];
            NSLog(@"评论数据%@",url);
        if (self.modelArray == nil) {
            self.modelArray = [NSMutableArray array];
        }
    [LORequestManger GET:url success:^(id response) {
        NSDictionary *Dic = (NSDictionary *)response;
        NSDictionary *DataDic = [Dic objectForKey:@"data"];
        NSMutableArray *dataArray = [DataDic objectForKey:@"recent_comments"];
        int a = 0;
        for (NSDictionary *temp in dataArray) {
            ++a;
            if (a > self.modelArray.count) {
                commentsMode *model = [commentsMode setHModeWithDic:temp];
                [self.modelArray addObject:model];
            }
        }
        NSLog(@"%ld",self.modelArray.count);
        [self.tableView reloadData];
        [self.activity stopAnimating];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"myCell";
    commentsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[commentsTableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:identifier];
    }
    commentsMode *model = self.modelArray[indexPath.row];

    [cell setContentWithMode:model];
    
    
    
    
    
    return cell;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

   
    return self.modelArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    commentsMode *model = self.modelArray[indexPath.row];
    CGFloat textHeight = [self YYHreturnStringHeight:model.commentContent Width:kScreenWdth - 20 Font:16];
    return 60 + textHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    return 28;
}



//计算高度
- (CGFloat)YYHreturnStringHeight:(NSString *)string Width:(CGFloat)Width Font:(int)font{
    
    CGRect frame = [string boundingRectWithSize:CGSizeMake(Width, 10000) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:font] forKey:NSFontAttributeName] context:nil];
    return frame.size.height;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
